import { Keywordss } from './keywords';
let _Keywordss = Keywordss;
/**
 * 拷贝下面两行到 mock/index.js中,即可支持客户端虚拟数据、免服务端连接
 * import KeywordsMock from './mallm/pub/keywordsMock';
 * KeywordsMock.bootstrap(mock);
 */
export default {
  /**
   * mock bootstrap
   */
  bootstrap(mock) {    
  
    //获取列表 模糊查询、根据关键字查询
    mock.onGet('/mallm/pub/keywords/listKey').reply(config => {
      let params = config.params;
      let mockKeywordss = _Keywordss.filter(keywords => {
      	if(params.keyword||params.keyword==''){ if( keywords.keyword.indexOf(params.keyword)>=0) return true;}
      	if(params.isHot||params.isHot==''){ if( keywords.isHot.indexOf(params.isHot)>=0) return true;}
      	if(params.isDefault||params.isDefault==''){ if( keywords.isDefault.indexOf(params.isDefault)>=0) return true;}
      	if(params.isShow||params.isShow==''){ if( keywords.isShow.indexOf(params.isShow)>=0) return true;}
      	if(params.sortOrder||params.sortOrder==''){ if( keywords.sortOrder.indexOf(params.sortOrder)>=0) return true;}
      	if(params.schemeUrl||params.schemeUrl==''){ if( keywords.schemeUrl.indexOf(params.schemeUrl)>=0) return true;}
      	if(params.id||params.id==''){ if( keywords.id.indexOf(params.id)>=0) return true;}
      	if(params.type||params.type==''){ if( keywords.type.indexOf(params.type)>=0) return true;}
      	if(params.deptid||params.deptid==''){ if( keywords.deptid.indexOf(params.deptid)>=0) return true;}
        return false;
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            data: mockKeywordss
          }]);
        }, 1000);
      });
    });
    
    

    //获取列表（分页/不分页）
    mock.onGet('/mallm/pub/keywords/list').reply(config => {
      let params = config.params;
      let mockKeywordss = _Keywordss.filter(keywords => {
      	var isTrue=false;
      	if(params.keyword||params.keyword==''){if(keywords.keyword.indexOf(params.keyword)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.isHot||params.isHot==''){if(keywords.isHot.indexOf(params.isHot)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.isDefault||params.isDefault==''){if(keywords.isDefault.indexOf(params.isDefault)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.isShow||params.isShow==''){if(keywords.isShow.indexOf(params.isShow)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.sortOrder||params.sortOrder==''){if(keywords.sortOrder.indexOf(params.sortOrder)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.schemeUrl||params.schemeUrl==''){if(keywords.schemeUrl.indexOf(params.schemeUrl)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.id||params.id==''){if(keywords.id.indexOf(params.id)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.type||params.type==''){if(keywords.type.indexOf(params.type)>=0){isTrue=true;}else{isTrue=false;}}
      	if(params.deptid||params.deptid==''){if(keywords.deptid.indexOf(params.deptid)>=0){isTrue=true;}else{isTrue=false;}}
      	return isTrue;
	  });
      if(!config.params.currentPage){ 
	      return new Promise((resolve, reject) => {
	        setTimeout(() => {
	          resolve([200, {
	            data: mockKeywordss
	          }]);
	        }, 1000);
	      });
      }
    
      let {pageSize,currentPage, total} = config.params;
      total = mockKeywordss.length;
      mockKeywordss = mockKeywordss.filter((keywords, index) => index < pageSize * currentPage && index >= pageSize * (currentPage - 1));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            pageInfo: {
            	total:total
            },
            data: mockKeywordss,
            tips:{
            	isOk:true,
            	msg:'查询成功'
            }
          }]);
        }, 1000);
      });
    });

    //删除
    mock.onPost('/mallm/pub/keywords/del').reply(config => {
      var params = JSON.parse( config.data );
      let {  id  } = params;
      _Keywordss = _Keywordss.filter(keywords => keywords.id !== id);
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //批量删除
    mock.onPost('/mallm/pub/keywords/batchDel').reply(config => {
      var params = JSON.parse( config.data );
      _Keywordss = _Keywordss.filter(keywords => !params.includes(keywords.id));
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
           tips:{
            	isOk:true,
            	msg:'删除成功'
            }
          }]);
        }, 500);
      });
    });

    //编辑
    mock.onPost('/mallm/pub/keywords/edit').reply(config => {
      var params = JSON.parse( config.data );
      let { keyword,isHot,isDefault,isShow,sortOrder,schemeUrl,id,type,deptid } = params;
      _Keywordss.some(keywords => {
        if (keywords.id === id) {
			keywords.keyword = keyword;
			keywords.isHot = isHot;
			keywords.isDefault = isDefault;
			keywords.isShow = isShow;
			keywords.sortOrder = sortOrder;
			keywords.schemeUrl = schemeUrl;
			keywords.id = id;
			keywords.type = type;
			keywords.deptid = deptid
          return true;
        }
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'编辑成功'
            }
          }]);
        }, 500);
      });
    });

    //新增
    mock.onPost('/mallm/pub/keywords/add').reply(config => {
      var params = JSON.parse( config.data );
      let { keyword,isHot,isDefault,isShow,sortOrder,schemeUrl,id,type,deptid } = params;
      _Keywordss.push({
		keyword:keyword,
		isHot:isHot,
		isDefault:isDefault,
		isShow:isShow,
		sortOrder:sortOrder,
		schemeUrl:schemeUrl,
		id:id,
		type:type,
		deptid:deptid
      });
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve([200, {
            tips:{
            	isOk:true,
            	msg:'新增成功'
            }
          }]);
        }, 500);
      });
    });

  }
};